'''
Created on 06/05/2011

@author: Eran_Z

populates the cache according to the input files 
'''

from RSM.algorithm import imdb_m
from RSM.my_movies.models import Movie, SearchResult
from django.shortcuts import render_to_response
from RSM.util import printDebug, isLegalName
import string
import imdb
import os


from RSM.settings import SITE_ROOT
#from RSM.algorithm.search.bingSearch.bingSearch import bingSearchResults
from RSM.algorithm.search.bossSearch.bossSearch import bossSearchResults
#from RSM.algorithm.search.deprecatedGoogleSearch.deprecatedGoogleSearch import googleSearchResults
# either googleSearchResults or bingSearchResults
# and switch between the 2 imports above.
searchEngine = bossSearchResults
basePath =  os.path.join(SITE_ROOT, 'algorithm/search/')
singlesFile = basePath + "best250.txt"
pairsFile = basePath + "best250pairs.txt"

def clearCache(part):
    if not part or part == 2:
        SearchResult.objects.all().delete()
    if not part or part == 1:
        Movie.objects.all().delete()

def populateCache(testMode, part=0):
    clearCache(part)
    printDebug('cache cleared')
    with open(singlesFile, "r") as f:
        lines = f.readlines()
        numLines = len(lines)
        if not part or part == 1:
            strOfNumLines = ' of ' + str(numLines)
            i=0
            for line in lines:
                i+=1
                if(not testMode):
                    printDebug(str(i) + strOfNumLines)
                lineList = line.split('\t')
                if (len(lineList) == 2):
                    if(testMode):
                        (link, year) = ('', 1900)
                    else:
                        (link, year) = imdb_m.getLinkAndYear(lineList[0])    
                    Movie(title=lineList[0], year=year,
                          searchSingle=lineList[1], link=link).save()
                elif (len(lineList)):
                    printDebug("ERROR: malformed single searches file. line: " + line)
            printDebug('done singles')
    if not part or part == 2:
        with open(pairsFile, "r") as f:
            i=1
            for line in f.xreadlines():
                if(not testMode):
                    printDebug(str(i) + ', ' + str(i+1) + ' of ' + str(numLines * (numLines - 1)))
                i+=2
                lineList = line.split('\t')
                if (len(lineList) == 3):
                    m1 = Movie.objects.get(title=lineList[0])
                    m2 = Movie.objects.get(title=lineList[1])
                    SearchResult(movie1=m1, movie2=m2, numResults=lineList[2]).save()
                    SearchResult(movie1=m2, movie2=m1, numResults=lineList[2]).save()
                elif (len(lineList)):
                    printDebug("ERROR: malformed double searches file. line: " + line)
        printDebug('done doubles')


def makeCache(request):
    printDebug('populateCache started')
    part = request.GET.get("part")
    if part:
        printDebug('only running part ' + part)
        populateCache(False, int(part))
    else:
        populateCache(False)
    printDebug('populateCache ended')
    return render_to_response('recommendation/doneReset.html', {'func': 'resetCache'})

def makeTestCache():
    printDebug('populateTestCache started')
    print os.getcwd()
    global singlesFile
    global pairsFile
    singlesFile = basePath + "best250Test.txt"
    pairsFile = basePath + "best250pairsTest.txt"
    print "changed file paths" + singlesFile + " &" + pairsFile
    populateCache(True)
    singlesFile = basePath + "best250.txt"
    pairsFile = basePath + "best250pairs.txt"
    print "restored file paths" + singlesFile + " &" + pairsFile
    printDebug('populateTestCache ended')



def filter250(topMovies):
    """
    returns a list that has all the names of movies from topMovies which are isLegalName.
    """
    res = []
    for m in topMovies:
        name = m['title']
        if isLegalName(name):
            name = string.replace(name,'?','')
            res.append(name)
    return res


def createBest250SearchPairs(topMovies):
    """
    This Function Creates file named best250Pairs.txt with pairs of best 250 movies names
    and their search results number   
    """
    with open(pairsFile, "w") as f:
        numMovies = len(topMovies)
        strOfAll = ' of ' + str(numMovies * (numMovies - 1) / 2)
        tot=0
        for i in range(numMovies):
            for j in range(i+1,numMovies):
                tot += 1
                printDebug(str(tot) + strOfAll)
                find_name = u'"%s" "%s"' % (topMovies[i],topMovies[j])
                res = searchEngine(find_name)
                if not res:
                    res = 1
                out = u'%s\t%s\t%d\n' % (topMovies[i],topMovies[j],res)
                f.write(out)
        f.flush()


def create250BestSearches(topMovies):
    """
    This Function Creates file named best250.txt with best 250 movies names 
    and their search results number
    """
    with open(singlesFile, "w") as f:
        numMovies = len(topMovies)
        strOfNumMovies = ' of ' + str(numMovies)
        i=0
        for m in topMovies:
            i+=1
            printDebug(str(i) + strOfNumMovies)
            res = searchEngine("\""+m+"\"")
            if not res:
                res = 1
            out = u'%s\t%d\n' % (m,res)
            f.write(out)
        f.flush()


def createBestMoviesFiles(request):
    i = imdb.IMDb()
    printDebug('CreateBestMoviesFiles started')
    topMovies = i.get_top250_movies() # if we want less: [:n]
    topMovies = filter250(topMovies)
    printDebug('topMovies list generated')
    printDebug('creating singles file')
    create250BestSearches(topMovies)
    printDebug('creating doubles file')
    createBest250SearchPairs(topMovies)
    printDebug('CreateBestMoviesFiles finished')
    return render_to_response('recommendation/doneReset.html', {'func': 'resetCacheFiles'})
